﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace cebao.Web.ForestStandard
{
    /// <summary>
    /// Getstandard 的摘要说明
    /// </summary>
    public class Getstandard : IHttpHandler, IRequiresSessionState
    {
        cebao.BLL.ForestStandard bll = new cebao.BLL.ForestStandard();
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "getlevel")
                getlevel(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "ddllist")
                ddllist(context);
        }
        public void getlevel(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string ForestPets = context.Request.QueryString["pets"];
                ForestPets = HttpUtility.UrlDecode(ForestPets);
                string Stage = context.Request.QueryString["stage"];
                Stage = HttpUtility.UrlDecode(Stage);
                string statisticunits = context.Request.QueryString["stati"];
                statisticunits = HttpUtility.UrlDecode(statisticunits);
                string aname = context.Request.QueryString["aname"];
                aname = HttpUtility.UrlDecode(aname);
                int asid = int.Parse(context.Request.QueryString["asid"]);
                string num = context.Request.QueryString["num"];
                string sql = string.Format(" ForestPets='{0}' and Stage='{1}' and statisticunits='{2}' and aname='{3}' and asid={4}", ForestPets, Stage, statisticunits, aname, asid);
                DataSet ds = bll.GetList(sql);
                decimal l = 0, c = 0,u = 0,mynum=0;
                if(num!="")
                mynum=decimal.Parse(num);
                string level = "";
                int fsid = 0;
                string json = "";
                if (ds.Tables[0].Rows.Count == 1)
                {
                    if (ds.Tables[0].Rows[0]["harmdegreelow"].ToString() != "")
                        l = decimal.Parse(ds.Tables[0].Rows[0]["harmdegreelow"].ToString());
                    if (ds.Tables[0].Rows[0]["harmdegreecenter"].ToString() != "")
                        c = decimal.Parse(ds.Tables[0].Rows[0]["harmdegreecenter"].ToString());
                    if (ds.Tables[0].Rows[0]["harmdegreeup"].ToString() != "")
                        u = decimal.Parse(ds.Tables[0].Rows[0]["harmdegreeup"].ToString());
                    if (mynum > u) level = "一级";
                    else if (mynum > c) level = "二级";
                    else if (mynum > l) level = "三级";
                    else level = "四级";
                    fsid = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                    json = "[{\"fsid\":\"" + fsid + "\",\"level\":\"" + level + "\",\"l\":\"" + l + "\",\"c\":\"" + c + "\",\"u\":\"" + u + "\"}]";
                    context.Response.Write(json);//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    json = "[{\"fsid\":\"0\",\"level\":\"没找到相关标准\",\"l\":\"0\",\"c\":\"0\",\"u\":\"0\"}]";
                    context.Response.Write(json);//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("");//返回给前台页面  
                context.Response.End();
            }
        }
        public void ddllist(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" uid={0} ", uid);
                DataSet ds = bll.GetList(strWhere.ToString());
                string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["asid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int mid = 0;
                string uidstr = context.Request.Form["Id"];
                if (uidstr != "")
                    mid = int.Parse(uidstr);
                string ForestPets = context.Request.Form["ForestPets"];
                string Stage = context.Request.Form["Stage"];
                string statisticunits = context.Request.Form["statisticunits"];
                string harmdegreelow = context.Request.Form["harmdegreelow"];
                string harmdegreecenter = context.Request.Form["harmdegreecenter"];
                string harmdegreeup = context.Request.Form["harmdegreeup"];
                string aname = context.Request.Form["aname"]; ;
                int asid = int.Parse(context.Request.Form["asid"]);

                cebao.Model.ForestStandard model = new cebao.Model.ForestStandard();
                model.ForestPets = ForestPets;
                model.Stage = Stage;
                model.statisticunits = statisticunits;
                model.harmdegreelow = harmdegreelow;
                model.harmdegreecenter = harmdegreecenter;
                model.harmdegreeup = harmdegreeup;
                model.aname = aname;
                model.asid = asid;
                model.Id = mid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                string ForestPets = context.Request.Form["ForestPets"]; 
                string Stage = context.Request.Form["Stage"]; 
                string statisticunits = context.Request.Form["statisticunits"]; 
                string harmdegreelow = context.Request.Form["harmdegreelow"]; 
                string harmdegreecenter = context.Request.Form["harmdegreecenter"]; 
                string harmdegreeup = context.Request.Form["harmdegreeup"]; 
                string aname = context.Request.Form["aname"]; 
                int asid = int.Parse(context.Request.Form["sname"]);
                cebao.Model.ForestStandard model = new cebao.Model.ForestStandard();
                model.ForestPets = ForestPets;
                model.Stage = Stage;
                model.statisticunits = statisticunits;
                model.harmdegreelow = harmdegreelow;
                model.harmdegreecenter = harmdegreecenter;
                model.harmdegreeup = harmdegreeup;
                model.aname = aname;
                model.asid = asid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string uname, utruename, st,pname="";
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    pname=uname = "";
                }
                else if(st=="petname")
                {
                    pname = uname;
                    utruename = uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" aname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" sname like '%{0}%' and ", utruename);
                }
                if (pname != "")
                {
                    strWhere.AppendFormat(" ForestPets like '%{0}%' and ", pname);
                }
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }  
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}